package com.hznu.jspFinal.controller;

import com.hznu.jspFinal.entity.School;
import com.hznu.jspFinal.myBeans.R;
import com.hznu.jspFinal.service.SchoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "School",description = "学院接口")
@RestController
@RequestMapping("/school")
public class SchoolController {
    @Autowired
    private SchoolService schoolService;

    @ApiOperation(value = "增加学院")
    @PostMapping("/addOne")
    public R addOne(@RequestBody School school){
        try{
            if (schoolService.findBySchoolName(school.getName()) != null){
                return R.error("该学校已经存在");
            }
            schoolService.insert(school);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "修改学院信息")
    @PostMapping("/updateBySchoolId")
    public R updateBySchoolId(@RequestBody School school){
        try{
            schoolService.update(school);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "删除学院")
    @GetMapping("/delete/{id}")
    public R deleteById(@PathVariable Integer id){
        try{
            schoolService.delete(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "查找学院")
    @GetMapping("/findBySchoolId/{id}")
    public R findBySchoolId(@PathVariable Integer id){
        try{
            School school = schoolService.findBySchoolId(id);
            return R.ok().put("data",school);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "查找所有的学院")
    @PostMapping("/findAll")
    public R findAll(){
        try{
            return R.ok().put("data",schoolService.findAll());
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}
